const path = require('path');
const fs = require('fs');
const { mdLoaders } = require('zerod-docs-conf/build/mdLoaders.js');
const docConfig = require('./src/docs.config');
const CompilationDoneStatus = require('zerod-docs-conf/build/CompilationDoneStatus');
function resolve(dir) {
    return path.join(__dirname, '.', dir);
}
const copyFolderName = 'src';
const alias = {};
const babelIncludes = [];
let sourceDirs = [copyFolderName];
if (docConfig.sourceDirs) {
    sourceDirs =
        typeof docConfig.sourceDirs === 'string'
            ? [docConfig.sourceDirs]
            : Array.isArray(docConfig.sourceDirs) && docConfig.sourceDirs.length
            ? docConfig.sourceDirs
            : sourceDirs;
}
sourceDirs.forEach((name) => {
    const _path = path.resolve(name, 'package.json');
    if (fs.existsSync(_path)) {
        const srcPack = require(_path);
        alias[srcPack.name] = path.resolve(name);
        babelIncludes.push(path.resolve(name));
    }
});

const config = {
    //追加合并webpack配置
    webpackMerger: {
        //入口js
        entry: {
            app: [resolve('node_modules/zerod-docs-conf/src/main.js')],
        },
        resolve: {
            alias,
        },
        plugins: [new CompilationDoneStatus()],
        // devServer: {
        //     historyApiFallback: {
        //         rewrites: [
        //             {
        //                 from: /(?<!^\/mockjs).*(?!\.html)/,
        //                 to: path.posix.join(docConfig.publicPath, 'index.html'),
        //             },
        //         ],
        //     },
        // },
    },

    platform: 'pc',
    //是否添加 pace.js(页面顶部进度条)
    pace: true,
    //是否antd的图标按需引入,在src/ant-icons.js
    'ant-icons': true,
    //根目录的哪个目录copy到node_modules,copy到node_modules的包名称是 copyFolderName/package.json的name，由npm run copy触发
    copyFolderName,
    //babel-loader 要包含的文件夹是哪些,false则不使用babel-loader
    'babel-includes': babelIncludes,
    //浏览器标签页小图标
    favicon: resolve('node_modules/zerod-docs-conf/src/assets/images/logo.png'),
    //scss变量提升文件,并且scss变量覆盖less变量
    scssVars: [resolve('zerod/ant-theme-vars.scss')],
    // //追加sass-loader的rule,false则不使用sass-loader
    // scssRule: {
    //     exclude: /node_modules[\\/](?!zerod|share|_zerod|_share)/,
    //     issuer: {
    //         exclude: /\.less$/,
    //     },
    // },
    // //追加less-loader的rule,false则不使用less-loader
    // lessRule: {
    //     exclude: /node_modules[\\/](?!antd|_antd|ant-mobile|_ant-mobile|zerod|share|_zerod|_share)/,
    // },
    //babel-plugin-react-css-modules的配置
    reactCssModulesOptions: {},
    //url-loader的参数配置
    urlLoaderOptions: {},
    //增加加载器，同 webpackMerger.module.rules
    loaders: [...mdLoaders],
    //将package.json的dependencies的依赖包预打包,npm run dll||dll-dev||dll-prod 触发
    dll: {
        //是否禁用dll
        disabled: false,
        //除了package.json的dependencies，还需包含
        include: [],
        //打包dll时从package.json的dependencies中不包含
        exclude: [],
        distEnv: false,
        //引入dll资源时使用的模式'production' || 'development'，默认：process.env.NODE_ENV
        usefulEnvName: 'production',
    },
    //html-webpack-plugin插件的追加配置 [] || (config,NODE_ENV)=>([])
    HtmlIncludeAssets: [],
    //copy-webpack-plugin插件的追加配置 [] || (config,NODE_ENV)=>([])
    CopyWebpackOptions: [],
    //开发模式配置
    dev: {
        template: resolve('node_modules/zerod-docs-conf/index.html'),
        // 不进行编译的静态文件目录
        assetsSubDirectory: 'static',
        // 前缀路径, npm run dev --basepath=/mypath/的basepath优先级大于此
        assetsPublicPath: docConfig.publicPath,
        //devServer的反代理配置
        proxyTable: [
            {
                context: [
                    '/elite-service',
                    '/webapi',
                    '/file-upload-service',
                    '/tibmas2-webapi',
                    '/resource-manager',
                    '/form-service',
                    '/api',
                ],
                target: 'http://gateway.zw-dev.com',
                changeOrigin: true,
            },
            {
                context: ['/login', '/logout'],
                target: 'http://172.16.8.231:30000',
                changeOrigin: false,
            },
            {
                context: ['/doc-service'],
                target: 'http://172.18.0.143:8991',
                changeOrigin: false,
            },
        ],
        // proxyTable: [
        //     // {
        //     //     context: [
        //     //         '/supervision-service',
        //     //         '/inspect-service'
        //     //     ],
        //     //     // target: 'http://szmz-gateway.zw-dev.com',
        //     //     target: 'http://172.20.3.220:9080',
        //     //     changeOrigin: false,
        //     // },
        //     {
        //         context: [
        //             '/file-upload-service',
        //             '/supervision-service',
        //             '/tibmas2-webapi',
        //             '/tibmas2-business-service',
        //             '/statistics-service',
        //             '/datacenter-service',
        //             '/inspect-service',
        //             '/system-service',
        //             '/form-service',
        //         ],
        //         target: 'http://szmz-gateway.zw-dev.com',
        //         changeOrigin: true,
        //     },
        //     {
        //         context: ['/zfzh-service'],
        //         target: 'http://gateway.zw-dev.com',
        //         changeOrigin: true,
        //     },
        //     {
        //         context: ['/webapi/login', '/webapi/logout'],
        //         target: 'http://172.16.8.232:31000/',
        //         changeOrigin: false,
        //     },
        //     {
        //         context: ['/zhzf/zfsjk/resource-manager'],
        //         target: 'http://zw.zw-dev.com',
        //         changeOrigin: true,
        //     },
        // ],
        host: '0.0.0.0',
        port: 33000,
        //启动服务后是否自动打开浏览器
        autoOpenBrowser: false,
        //出现编译器错误或警告时，在浏览器中显示全屏覆盖
        errorOverlay: true,
        //编译出错在右下角弹出提醒
        notifyOnErrors: true,
        poll: false,
        //是否编译时启用eslint
        useEslint: false,
        //追eslint-loader的rule
        eslintRule: {},
        devtool: 'cheap-module-eval-source-map',
        //css生成 SourceMap
        cssSourceMap: true,
    },
    //生产模式配置
    build: {
        // 根目录的index.html模板打包后的位置
        index: resolve('dist/index.html'),
        template: resolve('node_modules/zerod-docs-conf/index.html'),
        // 打包后所有文件的位置
        assetsRoot: resolve('dist'),
        // 不进行编译的静态文件目录
        assetsSubDirectory: 'static',
        // 前缀路径, npm run build --basepath=/mypath/的basepath优先级大于此
        assetsPublicPath: docConfig.publicPath,
        // 是否生成 SourceMap
        productionSourceMap: false,
        // https://webpack.js.org/configuration/devtool/#production
        devtool: 'source-map',
        // 是否打包压缩zip
        productionGzip: false,
        productionGzipExtensions: ['js', 'css'],
        // 打包完成后打开报告, npm run build --report 触发
        bundleAnalyzerReport: process.env.npm_config_report,
    },
    //覆盖webpack配置
    webpackReplacement: {},
};
module.exports = config;
